package com.syxp.pda.action.base;

import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.bean.FieldBean;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.web.PagingBean;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.Department;
import com.syxp.pda.service.base.DepartmentService;

public class DepartmentAction extends BaseAction{

	@Resource
	private DepartmentService departmentService ;

	public DepartmentService getDepartmentService() {
		return departmentService;
	}

	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	
	private String parentID ;
	
	private int departmentID ;
	
	
	
	private Department dept ;
	
	public Department getDept() {
		return dept;
	}

	public void setDept(Department dept) {
		this.dept = dept;
	}

	public int getDepartmentID() {
		return departmentID;
	}

	public void setDepartmentID(int departmentID) {
		this.departmentID = departmentID;
	}
	
	public String getParentID() {
		return parentID;
	}

	public void setParentID(String parentID) {
		this.parentID = parentID;
	}
	public String load() {
		List<Department> all = null ;
		if(parentID !=null) {
			QueryFilter filter = new QueryFilter() ;
			String[] temp = parentID.split("_") ;
			filter.addFilter(new FieldBean(temp[0],(Object)temp[1],temp[2])) ;
			all = departmentService.getDao().getAll(filter) ;
		}else{
			System.out.println("sdsdsd");
			all = departmentService.getDao().getAll() ;
		}
		JsonArray jarray = new JsonArray() ;
		JsonObject result = new JsonObject() ;
		for(Department dept : all) {
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("id", String.valueOf(dept.getDepartmentID())) ;
			//jobj.addProperty("depName", dept.getDepName()) ;
			jobj.addProperty("text", dept.getDepName()) ;
			jobj.addProperty("departmentCode", dept.getDepartmentCode());
			jobj.addProperty("depTel", dept.getDepTel());
			jobj.addProperty("remarks", dept.getRemarks());
			jarray.add(jobj) ;
		}
		this.jsonString = gson.toJson(jarray) ;
		System.out.println(jsonString) ;
		return SUCCESS ;
	}
	public String save() {
		try{
			Department d= departmentService.addService(dept) ;
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("id", String.valueOf(dept.getDepartmentID())) ;
			jobj.addProperty("text", dept.getDepName()) ;
			jobj.addProperty("success", true) ;
			this.jsonString=super.gson.toJson(jobj) ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String update() {
		try{
			System.out.println(dept.getParentID()) ;
			departmentService.updateService(dept) ;
			this.jsonString="{success:true}" ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String delete() {
		System.out.println(departmentID);
		try{
			departmentService.removeService(departmentID) ;
			this.jsonString="{success:true}" ;
		}catch(Exception e){
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		
		return SUCCESS ;
	}
	
	public String get()	{
		try{
			Department d = departmentService.loadService(departmentID) ;
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("departmentID", d.getDepartmentID()) ;
			jobj.addProperty("depName", d.getDepName()) ;
			jobj.addProperty("depTel", d.getDepTel()) ;
			jobj.addProperty("remarks", d.getRemarks()) ;
			jobj.addProperty("departmentCode", d.getDepartmentCode()) ;
			jobj.addProperty("parentID", d.getParentID()) ;
			jobj.addProperty("success", true) ;
			this.jsonString = gson.toJson(jobj) ;
			System.out.println(jsonString) ;
		}catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
}
